<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>模板列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/css/public.css" media="all">
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>新建模板</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">模板名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="tname" autocomplete="off" class="layui-input"
                                        lay-verify="required">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">模块类型</label>
                                <div class="layui-input-inline">
                                    <select name="ttype" lay-verify="">
                                        <option value="1">Api 模块</option>
                                        <option value="2">View 模块</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">模板地址</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="tpath" autocomplete="off" lay-verify="required"
                                        class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-new-btn">创 建</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm data-add-btn" lay-event="doc"> 模板创建规范 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-xs data-count-edit" lay-event="view">查看</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                <!-- <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="coder">代码</a> -->
            </script>

        </div>
    </div>
    <script src="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            table.render({
                elem: '#currentTableId',
                url: '{{domain}}/admin/api/layui_table?path=APP-api-admin-api-apis&func=template',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [
                    [{
                            type: "checkbox",
                            width: 50,
                            fixed: "left"
                        },
                        {
                            field: 'id',
                            width: 110,
                            title: '接口ID',
                            sort: true
                        },
                        {
                            field: 'name',
                            width: 180,
                            title: '模板名称'
                        },
                        {
                            field: 'type',
                            width: 140,
                            title: '模板类型'
                        },
                        {
                            field: 'savetype',
                            width: 140,
                            title: '储存方式'
                        },
                        {
                            field: 'path',
                            minWidth: 200,
                            title: '模板路径'
                        },
                        {
                            title: '操作',
                            minWidth: 50,
                            templet: '#currentTableBar',
                            fixed: "right",
                            align: "center"
                        }
                    ]
                ],
                parseData: function (res) {

                    // 将数据数字转为文字
                    for (var index in res.data) {
                        if (res.data[index].type == '1') {
                            res.data[index].type = 'Api 模板';
                        } else {
                            res.data[index].type = 'View 模板';
                        }

                        if (res.data[index].savetype == '1') {
                            res.data[index].savetype = '本地模板';
                        } else {
                            res.data[index].savetype = '云端模板';
                        }


                    }

                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.count,
                        data: res.data
                    };

                },
                page: false
            });

            // 监听新建操作
            form.on('submit(data-new-btn)', function (data) {

                var data = data.field;

                // 自动检查类型
                var reg = /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/i;

                var save = '1';
                if (reg.test(data.tpath)) {
                    save = '2';
                }

                // 发起新建请求
                $.post('{{domain}}/admin/api/apis/template?do=new', {
                    tname: data.tname,
                    ttype: data.ttype,
                    tsave: save,
                    tpath: data.tpath
                }, function (res) {
                    if (res.msg == '' && res.data == 'OK') {
                        window.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }
                }).error(function (res) {
                    layer.msg('操作异常：' + res.responseJSON.msg + '！');
                });

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'doc') { // 监听添加操作
                    var index = layer.open({
                        title: 'Template 模板创建规范',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['60%', '80%'],
                        content: 'http://lyapi-development.gitee.io/lymaster/#/apis/template',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'view') {

                    window.open('/admin/show?type=template&id=' + obj.data.id)

                } else if (obj.event === 'delete') {
                    layer.confirm('是否确定删除本模板？', function (index) {

                        $.post('{{domain}}/admin/api/apis/template?do=del', {
                            tpl: obj.data.id
                        }, function (res) {
                            if (res.data == 'OK') {
                                obj.del();
                            } else {
                                layer.msg(res.msg);
                            }
                        }).error(function (res) {
                            layer.msg('操作异常：' + res.responseJSON.msg + '！');
                        });
                        layer.close(index);
                    });
                }
            });

        });
    </script>
    <script>

    </script>

</body>

</html>